'use strict';
angular.module('gkwb')
    .controller('appCtrl', ['$rootScope','$scope', '$window', '$state', 'Student', function($rootScope,$scope, $window, $state, Student) {
        $scope.app = {
            title: '2016年湖北省普通高考网上报名'
        }
        $scope.getUsername = function() {
            return $window.sessionStorage.username || $window.sessionStorage.token;
        }
        $scope.logout = function() {
            Student.reset();
            $rootScope.student = null;
            $state.go('login');
            try 
            { 
                delete $window.sessionStorage.token;
                delete $window.sessionStorage.username;
            } 
            catch(e) 
            { 
                $window.sessionStorage.removeItem('token');
                $window.sessionStorage.removeItem('username');
            }
            
        }
    }])
    .controller('LoginCtrl', ['$scope', '$http', '$state', '$window', 'Student', '$rootScope', function($scope, $http, $state,
        $window, Student, $rootScope) {
        Student.reset();
        $scope.user = {
            userId: null,
            password: null
        };
        $scope.state = false;
        try 
        { 
            delete $window.sessionStorage.isReg;
        } 
        catch(e) 
        { 
            $window.sessionStorage.removeItem('isReg');
        }
        $scope.login = function() {
            $scope.state = true;

            $http.post('/view/login',$scope.user)
                .success(function(res) {
                    
                            $window.sessionStorage.token = res.userId;
                            Student.get(res.userId)
                                .then(function(data) {
                                    if (!res.pwdModifyCount || res.pwdModifyCount == 0) {
                                        $state.go('modifyPassword', {
                                            pwd: res.password
                                        });
                                    } else if (data.isReg) {
                                        $window.sessionStorage.isReg = true;
                                        $window.sessionStorage.username = data.name;
                                        $state.go('home.exam');
                                    } else {
                                        $state.go('main.promise');
                                    }
                                },function(err){
                                    $scope.state = false;
                                    if(err.status == 500)
                                        alert('数据错误');
                                });

                        
                  
                    
                })
                .error(function(res, status, headers, config, text) {
                    if (status == 0) {
                        alert('请求超时');
                    } else if(res.exception == 'javax.security.auth.login.LoginException'){
                        alert(res ? res.message : status);
                    }
                    else{
                        alert('无效用户名');
                    }
                    $scope.state = false;
                });
        }
    }])
    .controller('modifyPasswordCtrl', ['$scope', '$window', '$http', '$state', '$stateParams', function($scope, $window, $http, $state, $stateParams) {
        var userId = $window.sessionStorage.token;
        var oldPwd = $stateParams.pwd;
        $scope.pwd = {
            old: null,
            newPwd: null,
            newCheck: null
        }

        $scope.submitted = false;
        $scope.state = false;

        $scope.submit = function(form) {
            if ($scope.pwd.newPwd !== $scope.pwd.newCheck) {
                alert('两次输入密码不一致！');
                $scope.pwd.newPwd = null;
                $scope.pwd.newCheck = null;
            }
            if (form.$invalid) {
                $scope.submitted = true;
                alert('有未填写项目或密码长度不够');
            } else {
                $scope.state = true;
                $http({
                        method: 'PUT',
                        url: '/api/studentuser/' + userId,
                        data: $scope.pwd.newPwd
                    })
                    .success(function(data) {
                        if(data.isReg){
                            $state.go('home.exam');
                        }
                        else{
                            $state.go('main.promise');
                        }

                    })
                    .error(function(data, status) {
                        $scope.state = false;
                    })
            }
        }
    }])
    .controller('PayCtrl', ['$rootScope','$scope', '$http','$window','Student', function($rootScope,$scope, $http,$window,Student) {
        
        $rootScope.student = null;
        $scope.artPayItem = null;
        var examId = $window.sessionStorage.token;
        $scope.loading = Student.getFromServer(examId).then(function(data){
            $rootScope.student = $scope.student = data;
            angular.forEach(data.arts,function(item){
                if(item.cost > 0){
                    $scope.artPayItem = item.name;
                }
            })
        })

        $scope.isPaid = true;
        $http.get('/api/orders?examId=' + examId + '&status=PAYING')
                        .success(function(data) {
                            if (data.length > 0) {
                                $scope.error = '提醒：您提交过缴费单，请检查银行卡，确认是否已经扣款再进行缴费，以免重复缴费';
                            }
                        })

        
        $http.get('/view/pay?examId='+examId)
        .success(function(data){
            $scope.data = data;
            $rootScope.hide = true;
            $scope.isPaid = false;
        })
        .error(function(err){
            $rootScope.hide = true;
            $scope.error = err.message;
            $scope.isPaid = true;
        });

        $scope.paySubmit = function(form){
            alert(form);
        }
    }])
    .controller('HomeExamCtrl', ['$rootScope','$scope','$window','Student','$modal' ,function ($rootScope,$scope,$window,Student,$modal) {
        var examId = $window.sessionStorage.token;
        $scope.loading = Student.getFromServer(examId)
        .then(function(data){
            $rootScope.student = $scope.student = data;
            $rootScope.hide = true;
        },function(err){
            $scope.loading = null;
            alert(err.message);
        })

        var modal = $modal({scope: $scope, templateUrl: 'pages/tpl/modal.pay.html', show: false});
        $scope.goPay = function(){
            modal.$promise.then(modal.show);
        }
        $scope.reload = function(){
            modal.hide();
            $scope.loading = Student.getFromServer(examId)
        .then(function(data){
            $rootScope.student = $scope.student = data;
            $rootScope.hide = true;
        },function(err){
            $scope.loading = null;
            alert(err.message);
        })
        }
    }])
    .controller('HomeEditCtrl', ['$scope','$window','Student','$state','$stateParams' ,function ($scope,$window,Student,$state,$stateParams) {
        $scope.opt = $stateParams.opt;
        var examId = $window.sessionStorage.token;
        Student.get(examId)
        .then(function(data){
            $scope.student = data;
            $scope.master = angular.copy(data);
        })

        $scope.master = {};
        $scope.submitted = false;
        $scope.submit = function(form) {
            if(angular.equals($scope.master,$scope.student)){
                return $state.go('home.exam');
            }
            if (form.$invalid) {
                $scope.submitted = true;
                alert('有未填写或未选择的项目');
                return;
            }
            if ($scope.student.subjectType == 'ART' && $scope.student.arts[0].isEnabled) {
                var f = $window.confirm('确定不参加艺术专业报考吗？');
                if (!f) {
                    return;
                }
            }
            if ($scope.student.subjectType == 'SPORT' && $scope.student.sportSigned == 2) {
                var f = $window.confirm('确定不参加体育专业素质测试吗？');
                if (!f) {
                    return;
                }
            }
            $scope.loading = Student.save($scope.student)
                .then(function(res) {
                        alert('提交成功！');
                        $state.go('home.exam');
                    },
                    function(err) {
                        alert(err);
                    });
        }
    }]);